package com.bysc.cmsservice.aop;

import com.bysc.cmsservice.entity.CmsBanner;
import com.bysc.cmsservice.entity.CmsMember;
import com.bysc.cmsservice.entity.CmsTeacher;
import com.bysc.cmsservice.entity.vo.StudyFormVo;
import com.bysc.commonutils.Results;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;

import static com.bysc.commonutils.constant.RedisConstant.*;

@Aspect
@Component
@Slf4j
public class CmsAop {
    @Autowired
    private RedisTemplate redisTemplate;

    @Pointcut("execution(* com.bysc.cmsservice.controller.*.add*(..)) && !execution(* com.bysc.cmsservice.controller.StudyTypeController.*(..))")
    public void addPointCut() {}
    @Pointcut("execution(* com.bysc.cmsservice.controller.*.delete*(..))")
    public void deletePointCut() {}
    @Around("addPointCut()")
    public Object addAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("Method before execution: {}", joinPoint.getSignature().getName());

        Object result = joinPoint.proceed(); // 执行原方法

        if (result instanceof Results && ((Results) result).getSuccess()) {
            String fileUrl = getFileUrl(joinPoint.getArgs()[0]);
            if (fileUrl != null && !fileUrl.isEmpty()) {
                redisTemplate.opsForSet().add(FILE_URL_DB_RESOURCES, fileUrl);
            }

            Object id = ((Results) result).getData().get("id");
            if (id != null) {
                initRedis(joinPoint.getArgs()[0], id);
            }
        }

        log.info("Method after execution: {}", joinPoint.getSignature().getName());
        return result;
    }

    private String getFileUrl(Object arg) {
        if (arg instanceof CmsBanner) {
            return ((CmsBanner) arg).getImage();
        } else if (arg instanceof CmsMember) {
            return ((CmsMember) arg).getImage();
        } else if (arg instanceof CmsTeacher) {
            return ((CmsTeacher) arg).getImage();
        } else if (arg instanceof StudyFormVo) {
            return ((StudyFormVo) arg).getFileUrl();
        }
        return null;
    }

    /**
     * 操作缓存
     * @param arg
     * @param id
     */
    private void initRedis(Object arg, Object id) {
        String key = STUDY_DATA + id;
        String fileUrl = "";
        HashMap<String, Object> map = new HashMap<>();
        map.put(_LIKE, 0);
        map.put(_VIEW, 0);
        map.put(_COLLECT, 0);
        if (!fileUrl.isEmpty()) {
            redisTemplate.opsForSet().add(FILE_URL_DB_RESOURCES, fileUrl); // 图片地址缓存
        }

        if (!key.isEmpty()) {
            redisTemplate.opsForHash().putAll(key, map); // 初始化缓存
        }
    }
    @Around("deletePointCut()")
    public Object deleteAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("Method before execution: {}", joinPoint.getSignature().getName());

        Object result = joinPoint.proceed(); // 执行原方法

        if (result instanceof Results && ((Results) result).getSuccess()) {
            Object id = ((Results) result).getData().get("id");
            if (id != null) {
                String key = STUDY_DATA + id;
                redisTemplate.delete(key); // 删除缓存数据
            }
        }

        log.info("Method after execution: {}", joinPoint.getSignature().getName());
        return result;
    }

}
